package com.gms.server.service;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONWriter;

import com.gms.server.model.User;
import com.gms.tool.ModelToJsonFormat;
import com.gms.tool.ShareData;

/**
 * 
 * @author JBromo
 * 
 */
public class LoginUser extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("login");
		String password = request.getParameter("password");
		User user = new User(username, password);

		JSONWriter writer = new JSONWriter(response.getWriter());
		try {
			User usr = this.testUserLogin(user);
			// Check is user is null. Because if user is name.
			if (usr == null) {
				// If user is null we send and string to let know user is null
				writer.object();
				writer.key("loginStatus").value("no");
				writer.endObject();
			} else {
				// Else user is not null parse it

				// Transforme a model user to parson object
				ModelToJsonFormat.userToJson(user, writer);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	/**
	 * Get an Instance SQL connection
	 * 
	 * @return Object Connection
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private Connection getConnect() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		
		return DriverManager.getConnection("jdbc:mysql://" + ShareData.ADDRESS + "/" + ShareData.DBNAME, ShareData.DBLOGIN, ShareData.DBPASSWORD);
	}

	/**
	 * Verifie the user parameter if its correct
	 * 
	 * @param user
	 *            : User to check
	 * @return the user checked
	 * @throws Exception
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private User testUserLogin(User user) throws Exception, IllegalAccessException, ClassNotFoundException, SQLException {

		// The table name is : usertable and it has two columm : username and userpassword
		User usr = null;
		
		String requestSQL = "select *from usertable where username='"
				+ user.getUsername() + "' and userpassword='" + user.getPassword()
				+ "'";
		
		Connection connection = getConnect();
		
		Statement statement = connection.createStatement();
		
		ResultSet resultSet = statement.executeQuery(requestSQL);

		while (resultSet.next()) {
			// the get User parameter in the database
			usr = new User(resultSet.getString("username"),
					resultSet.getString("userpassword"));
		}
		return usr;
	}

}
